import psutil
def cpu_percent(interval=0.1):
    cpu_percent0 = psutil.cpu_percent(interval=interval)
    return cpu_percent0
# print(f"当前 CPU 使用率: {cpu_percent}%")

# 获取内存使用情况
def memory_percent():
    memory = psutil.virtual_memory()
    total_memory = memory.total / (1024 ** 3)  # 转换为 GB
    used_memory = memory.used / (1024 ** 3)# 转换为 GB
    free_memory=memory.free/(1024 ** 3)# 转换为 GB
    percent=memory.percent
    return percent,free_memory,used_memory,total_memory
    # print(f"总内存: {total_memory:.2f} GB，已使用内存: {used_memory:.2f} GB")

# 获取磁盘 I/O 统计信息
def disk_io_counters():
    disk_io = psutil.disk_io_counters()
    read_count = disk_io.read_count
    write_count = disk_io.write_count
    return read_count,write_count
# print(f"磁盘读操作次数: {read_count}，磁盘写操作次数: {write_count}")

def cpu_memory_monitor(interval=0.1):
    cpu_percent0 = psutil.cpu_percent(interval=interval)
    memory = psutil.virtual_memory()
    free_memory=memory.free/(1024 ** 3)# 转换为 GB
    percent = memory.percent
    return cpu_percent0,percent,free_memory